package com.example.serialportdemo;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class IPCService extends Service {
    private static final String TAG = "IPCService";
    private final String[] names = {"苹果","橘子","香蕉","苹果","苹果","苹果",};
    private MyBinder myBinder = new MyBinder();
    private class MyBinder extends Binder{
        @Override
        protected boolean onTransact(int code, @NonNull Parcel data, @Nullable Parcel reply, int flags) throws RemoteException {
            switch (code){
                case 0x001:{
                    data.enforceInterface("IPCService");
                    int num = data.readInt();
                    int num1 = data.readInt();
                    int num2 = data.readInt();
                    String test = data.readString();
                    reply.writeNoException();
                    reply.writeString(names[num] + " " + android.os.Process.myPid() + " " + num1 + " " + num2 + test);
                    reply.writeInt(1);
                    reply.writeString("收到");
                    return true;
                }
            }
            return super.onTransact(code, data, reply, flags);
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }
}
